/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjectMgtSystem.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.util.*;
import java.sql.*;
import ProjectMgtSystem.Utilities.*;
import ProjectMgtSystem.Entities.*;
import ProjectMgtSystem.Bean.*;

/**
 *
 * @author Nguyen Phu Cuong
 * Create date: 2011/07/14
 * Last updated: 2011/07/19
 */
public class EngineerAction extends LookupDispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String VIEW = "view";
    private final static String FAILURE = "failure";
    private final static String FIRST_UPDATE_ACTION = "viewSelectedID";

    /** Provides the mapping from resource key to method name.
     * @return Resource key / method name map.
     */
    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("engineer.insert", "insert");
        map.put("engineer.update", "update");
        map.put("engineer.delete", "delete");
        map.put("engineer.view", "view");
        map.put("engineer.select", "select");
        return map;
    }

    /**
     * Action called view action
     */
    public ActionForward view(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        EngineerActionBean enFrm = (EngineerActionBean) form;
        ArrayList<Engineer> enList = new java.util.ArrayList<Engineer>();
        enList = enFrm.view();
        request.setAttribute("engineerList", enList);
        //+++<Hien 07/25/2011: fix code>
        request.setAttribute("errorMessage", "");
        //---<Hien 07/25/2011: fix code>
        return mapping.findForward(VIEW); // Forward to admin/EngineerInfo.jsp
    }

    /** Action called on Add button click
     */
    public ActionForward insert(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        EngineerActionBean enFrm = (EngineerActionBean) form;
        String engineerid = enFrm.getEngineerid();
        String firstname = enFrm.getFirstname();
        String lastname = enFrm.getLastname();
        String major = enFrm.getMajor();
        String description = enFrm.getDescription();
        //+++<Hien 07/25/2011: fix code>
        //int rowIns = enFrm.insert(engineerid, firstname, lastname, major, description);
        //if (rowIns != 0) {
        String message = enFrm.insert(engineerid, firstname, lastname, major, description);
        //---<Hien 07/25/2011: fix code>
        ArrayList<Engineer> enList = new java.util.ArrayList<Engineer>();
        enList = enFrm.view();
        request.setAttribute("engineerList", enList);
        request.setAttribute("errorMessage", message);
        return mapping.findForward(VIEW);
        //+++<Hien 07/25/2011: fix code>
//        }
//        else 
//            return mapping.findForward(FAILURE);
        //---<Hien 07/25/2011: fix code>
    }

    /**
     * Action called on Update button click
     */
    public ActionForward select(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        EngineerActionBean enFrm = (EngineerActionBean) form;
        ArrayList<Engineer> enList = new java.util.ArrayList<Engineer>();
        enList = enFrm.select(enFrm.getEngineerid());
        request.setAttribute("enListUpdate", enList);
        return mapping.findForward(FIRST_UPDATE_ACTION); // Forward to admin/UpdateEngineerInfo.jsp
    }

    /** Action called on Submit button click at the Update Page
     */
    public ActionForward update(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws SQLException {
        EngineerActionBean enFrm = (EngineerActionBean) form;
        String engineerid = enFrm.getEngineerid();
        String firstname = enFrm.getFirstname();
        String lastname = enFrm.getLastname();
        String major = enFrm.getMajor();
        String description = enFrm.getDescription();
        int rowUp = enFrm.update(engineerid, firstname, lastname, major, description);
        //+++<Hien 07/25/2011: fix code>
        //if (rowUp != 0) {
        //---<Hien 07/25/2011: fix code>
            ArrayList<Engineer> enList = new java.util.ArrayList<Engineer>();
            enList = enFrm.view();
            request.setAttribute("engineerList", enList);
            request.setAttribute("errorMessage", String.valueOf(rowUp));
            return mapping.findForward(VIEW); //forward to admin/EngineerInfo.jsp
            //+++<Hien 07/25/2011: fix code>
//        } else {
//            return mapping.findForward(FAILURE);
//        }
        //---<Hien 07/25/2011: fix code>
    }

    /** Action called on Delete button click
     */
    public ActionForward delete(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws java.lang.Exception {
        EngineerActionBean enFrm = (EngineerActionBean) form;
        String engineerid = enFrm.getEngineerid();
        //+++<Hien 07/25/2011: fix code>
        //int rowDel = enFrm.delete(engineerid);
        String message = enFrm.delete(engineerid);       
        //if (rowDel != 0) {
             //---<Hien 07/25/2011: fix code>
            ArrayList<Engineer> enList = new java.util.ArrayList<Engineer>();
            enList = enFrm.view();
            request.setAttribute("engineerList", enList);
            request.setAttribute("errorMessage", message);
            return mapping.findForward(VIEW);            
         //+++<Hien 07/25/2011: fix code>   
//        } else {
//            return mapping.findForward(FAILURE);
//        }
        //---<Hien 07/25/2011: fix code>
    }
    /* And your JSP would have the following format for submit buttons:
    
    <html:form action="/test">
    <html:submit property="method">
    <bean:message key="button.add"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.edit"/>
    </html:submit>
    <html:submit property="method">
    <bean:message key="button.delete"/>
    </html:submit>
    </html:form>
     */
}
